﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection connChange = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["StaffID"] != null)
        {
            bUser.Text = Session["StaffID"].ToString();
        }
        else
        {
            if (Session["StudentID"] != null)
            {
                bUser.Text = Session["StudentID"].ToString();
            }
        }

    }


    protected void kiemtraAccounts(string ID, string OldPass, string NewPass)
    {

        if (Session["StaffID"] != null)
        {
            lbError.Text = "";
            string CommandString = "Select * from Staff where StaffID ='" + ID + "'";
            string UpdatePassword = " Update Staff set Password = '" + NewPass + "' Where StaffID = '" + ID + "'";
            SqlConnection connChange = new SqlConnection();
            connChange.ConnectionString = ConfigurationManager.ConnectionStrings["Institte_of_Fine_ArtsConnectionString"].ConnectionString;
            connChange.Open();
            SqlCommand cmdChange = new SqlCommand(CommandString, connChange);
            SqlDataAdapter daChange = new SqlDataAdapter(cmdChange);
            DataTable dtChange = new DataTable();
            daChange.Fill(dtChange);
            if ((dtChange.Rows.Count == 0 || dtChange.Rows[0][2].ToString() == OldPass))
            {
                lbError.Text = "Old Password not true!!!";
            }
            else
            {
                SqlCommand cmdUpdateChange = new SqlCommand(UpdatePassword, connChange);
                cmdUpdateChange.ExecuteNonQuery();
                Response.Write("<Script> alert('Change complete!!!');</Script>");
                Response.Redirect("~/Manager/HomePage.aspx");
            }
            connChange.Close();
        }
        else
        {
            lbError.Text = "";
            string CommandString = "Select * from Student where StudentID ='" + ID + "'";
            string UpdateMatKhau = " Update Student set Password = '" + NewPass + "' Where StudentID = '" + ID + "'";
            SqlConnection connChange = new SqlConnection();
            connChange.ConnectionString = ConfigurationManager.ConnectionStrings["Institute_of_Fine_ArtsConnectionString"].ConnectionString;
            SqlCommand cmdChange = new SqlCommand(CommandString, connChange);
            SqlDataAdapter daChange = new SqlDataAdapter(cmdChange);
            DataTable dtChange = new DataTable();
            daChange.Fill(dtChange);
            connChange.Open();
            if ((dtChange.Rows.Count == 0 || dtChange.Rows[0][2].ToString() != OldPass))
            {
                lbError.Text = "Old Password not true!!!";
            }
            else
            {
                SqlCommand cmdUpdateChange = new SqlCommand(UpdateMatKhau, connChange);
                cmdUpdateChange.ExecuteNonQuery();
                Response.Write("<Script> alert('Change complete!!!');</Script>");
                Response.Redirect("~/Student/HomePage.aspx");
            }
            connChange.Close();
        }
    }





    protected void btSubmit_Click(object sender, EventArgs e)
    {
        if (txtOldPassword.Text == "")
        {
            lbError.Text = "Old Password not null!";
            txtOldPassword.Focus();
        }
        else
        {
            if (txtNewPassword.Text == "")
            {
                lbError.Text = "NewPassword not null";
                txtNewPassword.Focus();
            }
            else
            {
                if (txtConfirm.Text != txtNewPassword.Text)
                {
                    lbError.Text = "Confirm Password isn't bland";
                    txtNewPassword.Focus();
                }
                else
                {

                    if (Session["StaffID"] != null)
                    {
                        string ID = (string)Session["StaffID"];
                        //string TaiKhoan = Session["TaiKhoan"].ToString();
                        string OldPass = txtOldPassword.Text;
                        string NewPass = txtNewPassword.Text;
                        kiemtraAccounts(ID, OldPass, NewPass);
                    }
                    else
                    {
                        string ID = (string)Session["StudentID"];
                        //string TaiKhoan = Session["TaiKhoan"].ToString();
                        string OldPass = txtOldPassword.Text;
                        string NewPass = txtNewPassword.Text;
                        kiemtraAccounts(ID, OldPass, NewPass);
                    }

                }
            }
        }
    }
    protected void btCancel_Click1(object sender, EventArgs e)
    {
        txtOldPassword.Text = "";
        txtNewPassword.Text = "";
        txtConfirm.Text = "";
        txtOldPassword.Focus();
    }
}